package com.ruoyi.system.task;

import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.ByHistoryFile;
import com.ruoyi.system.service.IByHistoryFileService;
import com.ruoyi.system.service.impl.EsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
public class ByFileScanTask  implements Runnable {


    @Override
    public void run() {
        // no matter what happened, can't effected the main process.
        try {
            //获取config路径
            String aimDir = RuoYiConfig.getAimPath();
            Path startPath = Paths.get(aimDir);
            IByHistoryFileService byHistoryFileService = SpringUtils.getBean(IByHistoryFileService.class);
            Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    ByHistoryFile byHistoryFile = new ByHistoryFile();
                    byHistoryFile.setFilename(file.getFileName().toString());
                    byHistoryFile.setFilepath(file.toString());
                    byHistoryFile.setPrefix(FileUtil.extName(file.getFileName().toString()));
                    byHistoryFileService.insertByHistoryFile(byHistoryFile);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
